// 封装分类数据业务相关代码
import { ref, onMounted } from "vue";
import { getSecondCategoryList } from "@/apis/category";
import { useRoute, onBeforeRouteUpdate } from "vue-router";

export const useCategory = () => {
    const route = useRoute();
    const categoryInfo = ref({});

    // 获取二级分类数据
    const getSecCategoryList = async (id = route.params.id) => {
        const res = await getSecondCategoryList(id);
        categoryInfo.value = res.result;
    };

    onMounted(() => {
        getSecCategoryList();
    });

    // 期望在路由参数变化的时候，可以把分类数据接口重新发送
    onBeforeRouteUpdate((to) => {
        getSecCategoryList(to.params.id);
    });
    return {
        categoryInfo
    }
}